package com.bjtang.book.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bjtang.book.util.ApplicationUtils;

/**
 * 登录拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 给嗅探OPTIONS放行
		if (request.getMethod().equals("OPTIONS"))
			return true;

		// 访问者的IP
		// String ip = request.getRemoteAddr();
		// 得到用户的浏览器名
		// String userbrowser = BrowserUtil.getOsAndBrowserInfo(request);
		// 访问地址
		String str = request.getRequestURL().toString();
		// url.indexOf("book") 如果没找到则返回-1,找到了返回第一次出现所在位置的下表
		String url = str.substring(str.indexOf("book") + 4);

		// 获取Session
		HttpSession session = request.getSession();
		Object userId = session.getAttribute("USER_ID_SESSION");
		System.err.println("用户id为" + userId + "  " + ApplicationUtils.getRequest().getSession().toString());
		// 放行swagger
		if (url.indexOf("/swagger") == 0)
			return true;
		// 放行swagger
		if (url.indexOf("/webjars/") == 0)
			return true;
		// 放行登录
		if (url.indexOf("/login/") == 0)
			return true;
		// 放行错误
		if (url.indexOf("/err/") == 0)
			return true;
		// 放行公开
		if (url.indexOf("/open/") == 0)
			return true;
		if (userId == null) {
			response.sendRedirect("/book" + "/err/401/");
			return false;
		}
//		if (userId != null) {
//			// response.sendRedirect("/book" + "/err/401/");
//			return true;
//		}

		System.err.println("访问地址为" + url);
		System.err.println("访问方法为" + request.getMethod());
		System.err.println("访问合法");
		return true;

		// 测试状态都放行
		// 不符合条件的给出提示信息，并转发到登录页面
		// response.sendRedirect("/denglu");
		// response.sendRedirect("/huatec/static/html/login.html");
		// request.getRequestDispatcher("index").forward(request, response);
//		System.err.println("访问地址为" + url);
//		System.err.println("访问方法为" + request.getMethod());
//		System.err.println("访问非法");
//		response.sendRedirect("/book" + "/err/403/");
//		return false;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (handler instanceof HandlerMethod) {
//	            HandlerMethod handlerMethod = (HandlerMethod) handler;
			// Method method = handlerMethod.getMethod();
			// 保存日志信息
//	            sysLog.setRemark(method.getName());
//	            sysService.addLog(sysLog);

		} else {
//	            String uri = request.getRequestURI();
//	            sysLog.setRemark(uri);
//	            sysService.addLog(sysLog);
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
